package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserSecretVolume;

import java.sql.*;

public class SecretVolumeDaoImpl extends TempDataObjectDao<UserSecretVolume> {

	public UserSecretVolume getUserSecretVolume(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_u_secret_volume where userId=? ORDER BY `startDate` DESC LIMIT 1";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserSecretVolume data = parse(rs);
				return data;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	@Override
	protected boolean addData(UserSecretVolume data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_secret_volume(startDate,userId,`no`,exp,integral,getRewardLevel,dateLength,isBuy,updateTime) VALUES(?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setDate(i++, new Date(data.getStartDate()));
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getNo());
			call.setLong(i++, data.getExp());
			call.setLong(i++, data.getIntegral());
			call.setString(i++, data.getGetRewardLevel());
			call.setInt(i++, data.getDateLength());
			call.setBoolean(i++, data.isBuy());
			call.setDate(i++, new Date(data.getUpdateTime()));
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}
	/*
	优化的表结构记录一下，不然到时候和代码不知道去哪找 20221110
ALTER TABLE `t_u_secret_volume` DROP PRIMARY KEY;
CREATE INDEX `idx_userIdNoStartDate` ON `t_u_secret_volume`(`userId`, `no`, `startDate`) USING BTREE ;
	* */

	@Override
	protected boolean updateData(UserSecretVolume data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_secret_volume SET startDate=?,exp=?,integral=?,getRewardLevel=?,dateLength=?,isBuy=?,updateTime=? " +
				"WHERE userId = ? AND `no` = ? AND startDate = ?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setDate(i++, new Date(data.getStartDate()));
			call.setLong(i++, data.getExp());
			call.setLong(i++, data.getIntegral());
			call.setString(i++, data.getGetRewardLevel());
			call.setInt(i++, data.getDateLength());
			call.setBoolean(i++, data.isBuy());
			call.setDate(i++, new Date(data.getUpdateTime()));
			call.setLong(i++, data.getUserId());
			call.setLong(i++, data.getNo());
			call.setDate(i++, new Date(data.getStartDate()));
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserSecretVolume parseData(ResultSet rs) throws SQLException {
		UserSecretVolume data = new UserSecretVolume();
		data.setUserId(rs.getLong("userId"));
		data.setNo(rs.getInt("no"));
		data.setStartDate(rs.getDate("startDate").getTime());
		data.setExp(rs.getLong("exp"));
		data.setIntegral(rs.getLong("integral"));
		data.setGetRewardLevel(rs.getString("getRewardLevel"));
		data.setDateLength(rs.getInt("dateLength"));
		data.setIsBuy(rs.getBoolean("isBuy"));
		data.setUpdateTime(rs.getDate("updateTime").getTime());
		return data;
	}
}
